﻿using Enum;
using MediatR;
using Practice.Common.Domains;
using Practice.Common.Dto;
using Practice.Common.Infrastructure.Interfaces;

namespace Practice.API.Application.Commands.UserW
{
    public class UserLoginHandler:IRequestHandler<UserLoginCommand,Result>
    {
        public readonly IBaseRepository<Users> UsersRepository;

        public UserLoginHandler(IBaseRepository<Users> usersRepository)
        {
            UsersRepository = usersRepository;
        }

        public async Task<Result> Handle(UserLoginCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonEnumCode.Search };
            try
            {
                var list = UsersRepository.GetAll().Where(x => x.UserName == request.UserName && x.Pwd == request.Pwd).ToList();
                if (list != null)
                { 
                    result.Data = list;
                    result.Code= (int)CommonEnumCode.Success;
                    result.token = request.token;
                }

            }
            catch (Exception)
            {

                result.Msg = "登录失败";
            }
            return result;
        }
    }
}
